/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Amenity;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IAmenityService;
import org.cx1.service.impl.DAmenityService;
import org.cx1.service.impl.MAmenityService;
import org.cx1.web.controller.action.AmenityAction;
import org.cx1.web.utils.SessionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author ECH
 */
public class AmenityController extends BaseController {
    
    private IAmenityService amenityService = new DAmenityService();
    
    public AmenityController() {
        
    }
    
    protected ModelAndView handleRequestInternal( HttpServletRequest request,
                                                  HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();        
        
        String userID = SessionUtils.getUserID(request.getSession());     
        if (StringUtils.isEmpty(userID)) {
            mav.setView(new RedirectView("home.htm"));
            return mav;
        } 
        if (!StringUtils.isEmpty(userID)) {
            map.put("loggedin", true);
        }      
        
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        if (ServletFileUpload.isMultipartContent(request)) {
            parameterMap = this.getMultipartFormParams(request);
        } 
        else {
            parameterMap = this.getFormParams(request);
        }
        
        Map<String, String> formfields = (Map<String, String>) parameterMap.get("formfields");
        int action = StringUtils.isEmpty(formfields.get("a")) ? 0 : Integer.parseInt(formfields.get("a"));
        
        switch (action) {
            case AmenityAction.READ_ALL: {
                readAll(mav, map, request);
                break;
            }
            case AmenityAction.READ: {
                read(mav, map, request);
                break;
            }
            case AmenityAction.CREATE: {
                create(mav, map, request);
                break;
            }
            case AmenityAction.UPDATE: {
                update(mav, map, request);
                break;
            }    
            case AmenityAction.DELETE: {
                delete(mav, map, request);
                break;
            }
            default: {
                readAll(mav, map, request);
                break;
            }
        }  
        
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        map.put("props", this.getSystemProperties());
        mav.addObject("model", map);
        return mav;
    }
    
    private void readAll(ModelAndView mav, Map map, HttpServletRequest request) {
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        ServiceResponse<List<Amenity>> response = amenityService.readAll(StringUtils.isEmpty(offset) ? 0 : Integer.parseInt(offset), StringUtils.isEmpty(limit) ? 0 : Integer.parseInt(limit));
        map.put("amenities", response.getResponse());
        mav.setViewName("amenities");    

    }
    
    private void read(ModelAndView mav, Map map, HttpServletRequest request) {
        String id = request.getParameter("id");
        ServiceResponse<Amenity> response = amenityService.read(id);
        map.put("amenity", response.getResponse());
        mav.setViewName("amenity");
    }
    
    private void create(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {                     
            String amenityTypeID = request.getParameter("amenityTypeID");
            
            String title = request.getParameter("title");
            String description = request.getParameter("description");
            String notes = request.getParameter("notes");
                    
            Amenity amenity = new Amenity();       
            amenity.setAmenityTypeID(amenityTypeID);
            
            amenity.setTitle(title);
            amenity.setDescription(description);
            amenity.setNotes(notes);
            
            ServiceResponse<String> response = amenityService.create(amenity);
            map.put("amenity", response.getResponse());
            mav.setView(new RedirectView("amenity.htm?a="+AmenityAction.READ_ALL+"&id="+response.getResponse()));   
        }
        else {
            //CX: get amenity choices
            //CX: get amenity choices
            mav.setViewName("amenityadd");
        }
    }
    
    
    private void update(ModelAndView mav, Map map, HttpServletRequest request) {
        if (!StringUtils.isEmpty(request.getParameter("s"))) {             
            String id = request.getParameter("id");            
            String amenityTypeID = request.getParameter("amenityTypeID");
            
            String title = request.getParameter("title");
            String description = request.getParameter("description");
            String notes = request.getParameter("notes");
                        
            ServiceResponse<Amenity> response1 = amenityService.read(id);              
            Amenity amenity = response1.getResponse();          

            if (amenity != null) {         
                amenity.setAmenityTypeID(amenityTypeID);
                
                amenity.setTitle(title);
                amenity.setDescription(description);
                amenity.setNotes(notes);

                ServiceResponse<Boolean> response = amenityService.update(amenity);
                map.put("amenity", response.getResponse());
                mav.setView(new RedirectView("amenity.htm?a="+AmenityAction.READ_ALL+"&id="+id));
            }
            else {
                mav.setView(new RedirectView("amenity.htm"));                
            }
        }        
        else {                       
            String id = request.getParameter("id");    
            ServiceResponse<Amenity> response = amenityService.read(id);              
            map.put("amenity", response.getResponse());      
            mav.setViewName("amenityedit");
        }
    }
    
    private void delete(ModelAndView mav, Map map, HttpServletRequest request) {        
        String id = request.getParameter("id");
        ServiceResponse<Boolean> response = amenityService.delete(id);        
        mav.setView(new RedirectView("amenity.htm?a="+AmenityAction.READ_ALL));
    }
    
}